package edu.tapestry.iddb.client.pages.profil;

import iddb.ConnectResults;
import iddb.ImUserPOJO;
import iddb.PluginIdentificator;
import iddb.ProfilInfo;
import iddb.ProfilPOJO;

import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import edu.tapestry.iddb.client.helpers.IMIdentificator;
import edu.tapestry.iddb.client.helpers.PI;
import edu.tapestry.iddb.client.pages.ConnectResultsPage;
import edu.tapestry.iddb.client.pages.buddyIM.ListBuddyIM;
import edu.tapestry.iddb.client.pages.groupIM.ListGroupIM;
import edu.tapestry.iddb.client.pages.message.RecievedListMessage;
import edu.tapestry.iddb.client.webService.CommandsGenerator;
import edu.tapestry.iddb.client.webService.IDDBProxy;

public class DetailsProfil {

	@ApplicationState
	private IDDBProxy proxy;

	@Property
	private ImUserPOJO user;

	@Property
	@Persist
	private ProfilPOJO profil;

	@InjectPage
	private ListGroupIM listGroupsPage;
	
	@InjectPage
	private ListBuddyIM listBuddiesPage;
	
	@InjectPage
	private RecievedListMessage listMessagesPage;

	@InjectPage
	private ConnectResultsPage resultsPage;

	@Parameter
	private IMIdentificator identificator;
	
	
	@Inject
	private BeanModelSource beanModelSource;

	@Inject
	private ComponentResources resources;

	private BeanModel<ImUserPOJO> model;

	Object initialize(String profilname) {

		ProfilInfo profilInfo = ((ProfilInfo) proxy.execute(CommandsGenerator
				.createGetProfile(profilname)));

		this.profil = (ProfilPOJO) profilInfo.getObject();

		return this;
	}

	void pageLoaded() {
		this.model = beanModelSource.create(ImUserPOJO.class, true, resources
				.getMessages());
		this.model.add("details", null);
	}

	public Object onActionFromGroupsLink(PI ident) {
		return listGroupsPage.initialize(ident.getIdent());
	}

	public Object onActionFromBuddiesLink(PI ident) {
		return listBuddiesPage.initialize(ident);
	}
	
	public Object onActionFromConnectLink(IMIdentificator ident) {
		ConnectResults results = (ConnectResults) proxy
				.execute(CommandsGenerator.simpleConnCmd(ident.getUsername(),
						ident.getType(), ident.getSrvAddr()));

		return resultsPage.initialize(results);
	}
	
	public Object onActionFromMessagesLink(PI ident){
		return listMessagesPage.initialize(ident.getIdent());
	}

	public List<ImUserPOJO> getUsers() {
		return profil.getImusers();
	}

	public ProfilPOJO getProfil() {
		return this.profil;
	}
	
	public IMIdentificator getIdentificator(){
		identificator = new IMIdentificator(user);
		return identificator;
	}
	
	public PI getPluginIdentificator(){
		PluginIdentificator pi = new PluginIdentificator();
		pi.setSrvAddr(user.getSrvAddr());
		pi.setType(user.getImType());
		pi.setUsername(user.getUsername());
		
		return new PI(pi);
		
	}

}
